// pages/mine/vip/index.js
const app = getApp();
const time = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    reg_avatar_url: wx.getStorageSync('reg_avatar_url'),

    // 模态框
    flag_detail: true,
    sjcNow: app.globalData.sjcNow
  },
  //去web页面
  go_url(e){
    console.log(e)
    const that=this;
    let url=e.currentTarget.dataset.url;
    wx.navigateTo({
      url: '../../protocol/protocol?url=' + url+'&title=促销详情'
    })
  },
  //去购买明细
  buy_detail(){
    wx.navigateTo({
      url: 'buyDetail/buyDetail',
    })
  },
  //隐藏模态框
  modal_hide() {
    const that = this;
    that.setData({
      flag_detail: true,
    })

  },
  //去认证
  go_approve(){
    wx.showModal({
      content: '请下载直播之家APP进行认证',
      showCancel: false,
      confirmText: '确定',
      confirmColor: '#d0a76c',
      success: function(res) {},
    })
    // wx.navigateTo({
    //   url: '../CompanyBasis/CompanyBasis?flag=vip_flag',
    // })
  },
  //company_info
  getUserInfo(){
    const that=this;
    wx.request({
      url: app.globalData.url + 'api/common/register/info',
      data: {
        token:app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
        let user = e.data.data
        that.setData({
          user: user,
        })
      },
    })
  },
  //去看详情
  check_detail(e) {
    // console.log(e)
    const that = this;
    that.setData({
      flag_detail: false,
      name: e.currentTarget.dataset.name,
      detail: e.currentTarget.dataset.detail,
    })
  },
  //切换swiper
  cardChange(e) {
    // console.log(e)
    const that = this;
    let current = e.detail.current;
    // console.log(current)
    that.setData({
      current: current
    })
  },
  //获取banner
  getBanner(){
    const that=this;
    wx.request({
      url: app.globalData.url + 'api/vip/banner',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
        let banner = e.data.data.banner;
        that.setData({
          banner: banner,
        })
      },
    })
  },
  //获取vip套餐列表
  getViplist() {
    const that = this;
    wx.request({
      url: app.globalData.url + 'api/vip/packages/list',
      data: {
        token: app.globalData.token,
        sys: 1,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)
        // console.log('reg_avatar_url+' + that.data.reg_avatar_url)
        let vipList = e.data.data.packages;
        vipList.forEach(function(value, index) {
          value.flag_time = time.formatTimeTwo(value.expire_time, 'Y-M-D');
        })
        // console.log(vipList)
        that.setData({
          vipList: vipList,
        })
      },

    })
  },
  //获取vip权益
  getVipequity() {
    const that = this;
    wx.request({
      url: app.globalData.url + 'api/vip/advantage/list',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)
      },

    })
  },
  //vip权益说明
  getVipstate() {
    const that = this;
    wx.request({
      url: app.globalData.url + 'api/vip/advantage/explain',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
        let advantages = e.data.data.advantages;
        that.setData({
          advantages: advantages,
        })
      },

    })
  },
  //获取详情
  getInfo(e) {
    const that = this;
    // console.log(e)
    that.setData({
      price: e.currentTarget.dataset.price,
      price_sales: e.currentTarget.dataset.price_sales ? e.currentTarget.dataset.price_sales : "",
      valid_month: e.currentTarget.dataset.valid_month,
      id: e.currentTarget.dataset.id,
      is_sales:e.currentTarget.dataset.is_sales,
    })
  },
  // 付钱
  click_pay(e) {
    console.log(e)
    let that = this;
    const encryptedData = e.detail.encryptedData;
    const iv = e.detail.iv;
    var valid_month =
      that.setData({
        encryptedData: encryptedData,
        iv: iv,
      })
    var timer = setTimeout(function() {
      wx.login({
        success(e) {
          // console.log(e.code);
          that.setData({
            code: e.code,
          })
          clearTimeout(timer)
          //原始支付
          // that.get_openId(that.data.code, that.data.encryptedData, that.data.iv)
          //新支付方式
          // var openId = wx.getStorageSync('openId');
          // that.pay_package(openId);
          if (wx.getStorageSync('openId')) {
            const openId = wx.getStorageSync('openId');
            that.pay_package(openId);
          } else {
            wx.showModal({
              content: '请先进行授权',
              showCancel: true,
              cancelText: '取消',
              cancelColor: '#333333',
              confirmText: '好的',
              confirmColor: '#d0a76c',
              success: (e) => {
                if (e.confirm) {
                  wx.navigateTo({
                    url: '../../author/author',
                  })
                }
              }
            })
          }

        }
      })
    }, 0)

  },
  //获取openId >> 
  get_openId(code, encryptedData, iv) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/sign/login/getWxLogin',
      data: {
        code: code,
        encryptedData: encryptedData,
        iv: iv,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)
        let openId = e.data.data.getuserinfo.openId
        that.pay_package(openId)
      },
    })
  },
  //提交后台获取
  pay_package(openId) {
    let that = this,
      package_id = this.data.id,
      package_duration = this.data.valid_month,
      price = this.data.price,
      price_sales = this.data.price_sales,
      is_sales=this.data.is_sales;
    if (is_sales) {
      var pay_price = this.data.price_sales;
    } else {
      var pay_price = this.data.price;
    }
    wx.request({
      url: app.globalData.url + 'api/vip/create/order',
      data: {
        token: app.globalData.token,
        package_id: package_id,
        package_duration: package_duration,
        pay_price: pay_price,
        pay_method: "2",
        bought_channel: 3,
        pay_status: 0,
        openId: openId,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)
        let _appId = e.data.data.appId,
          _nonceStr = e.data.data.nonceStr,
          _package = e.data.data.package,
          _paySign = e.data.data.paySign,
          _signType = e.data.data.signType,
          _timeStamp = e.data.data.timeStamp;
        wx.requestPayment({
          appId: _appId,
          nonceStr: _nonceStr,
          package: _package,
          paySign: _paySign,
          signType: _signType,
          timeStamp: _timeStamp,
          success(e) {
            console.log(e)
            if (e.errMsg =="requestPayment:ok"){
              wx.showModal({
                content: '支付完成',
                showCancel: false,
                confirmText: '好',
                confirmColor: '#d0a76c',
                success: function (e) {
                  // console.log(e)
                  that.getViplist()
                  if (e.confirm) {
                  }
                },
              })
            }else{
              wx.showModal({
                content: '支付失败',
                showCancel: false,
                confirmText: '好',
                confirmColor: '#d0a76c',
                success: function (e) {
                  // console.log(e)
                  that.getViplist()
                  if (e.confirm) {
                  }
                },
              })
            }
            
          }
        })
      },
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(this.data.sjcNow)
    this.getBanner();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getViplist();
    this.getVipequity();
    this.getVipstate();
    this.getUserInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})